package cn.t.repository.impl;

import cn.t.converter.api.MqProcessLogConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.common.MqProcessLogDO;
import cn.t.persistence.entity.MqProcessLogPO;
import cn.t.persistence.mapper.MqProcessLogMapper;
import cn.t.repository.common.MqProcessLogRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * mq消息日志表数据仓库
 */
@Slf4j
@Repository
@AllArgsConstructor
public class MqProcessLogRepositoryImpl extends RepositoryImpl<MqProcessLogMapper, MqProcessLogPO, MqProcessLogConverter, MqProcessLogDO> implements MqProcessLogRepository {


    @Override
    public List<MqProcessLogDO> findByModel(MqProcessLogDO rMqProcessLogDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<MqProcessLogPO> list = getWrapper(rMqProcessLogDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<MqProcessLogPO> getWrapper(MqProcessLogDO mqProcessLogDO) {
        LambdaQueryChainWrapper<MqProcessLogPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(mqProcessLogDO.getQueue())) {
            wrapper.eq(MqProcessLogPO::getQueue, mqProcessLogDO.getQueue());
        }
        if (!ObjectUtils.isEmpty(mqProcessLogDO.getBusinessKey())) {
            wrapper.eq(MqProcessLogPO::getBusinessKey, mqProcessLogDO.getBusinessKey());
        }
        if (!ObjectUtils.isEmpty(mqProcessLogDO.getMqContent())) {
            wrapper.eq(MqProcessLogPO::getMqContent, mqProcessLogDO.getMqContent());
        }
        if (ObjectUtils.isEmpty(mqProcessLogDO.getRequest())) {
            wrapper.eq(MqProcessLogPO::getRequest, mqProcessLogDO.getRequest());
        }
        if (!ObjectUtils.isEmpty(mqProcessLogDO.getResponse())) {
            wrapper.eq(MqProcessLogPO::getResponse, mqProcessLogDO.getResponse());
        }
        if (!ObjectUtils.isEmpty(mqProcessLogDO.getProcessTime())) {
            wrapper.eq(MqProcessLogPO::getProcessTime, mqProcessLogDO.getProcessTime());
        }
        if (!ObjectUtils.isEmpty(mqProcessLogDO.getGmtCreated())) {
            wrapper.between(MqProcessLogPO::getGmtCreated, mqProcessLogDO.getGmtCreatedStart(), mqProcessLogDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(mqProcessLogDO.getGmtModified())) {
            wrapper.between(MqProcessLogPO::getGmtModified, mqProcessLogDO.getGmtModifiedStart(), mqProcessLogDO.getGmtModifiedEnd());
        }
        wrapper.in(CollectionUtils.isEmpty(mqProcessLogDO.getBusinessKeys()), MqProcessLogPO::getBusinessKey, mqProcessLogDO.getBusinessKeys());

        return wrapper;
    }

    @Override
    public long count(MqProcessLogDO rMqProcessLogDO) {
        return getWrapper(rMqProcessLogDO).count();
    }
}
